import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:my_flutter_core/extensions/string_extension.dart';
import 'package:my_flutter_core/services/navigator_helper.dart';
import 'package:my_flutter_core/utils/date_time_utils.dart';
import 'package:my_flutter_core/widgets/self_rotating.dart';

/// Toast
void showToast(String? msg) {
  if (msg.isNotNullOrEmpty) {
    Fluttertoast.showToast(
      msg: msg!,
      toastLength: Toast.LENGTH_LONG,
      backgroundColor: Colors.grey[600],
      textColor: Colors.white,
    );
  }
}

/// Alert Dialog
Future<void> showAlert(
  BuildContext context,
  String msg, {
  Key? key,
  VoidCallback? onTap,
}) {
  return showDialog(
    context: context,
    builder: (context) {
      return AlertDialog.adaptive(
        key: key,
        title: const Text('提示'),
        content: SingleChildScrollView(child: Text(msg)),
        actions: [
          TextButton(
            child: const Text('确认'),
            onPressed: () {
              NavigatorHelper.pop(context);
              onTap?.call();
            },
          ),
        ],
      );
    },
  );
}

/// Confirm Dialog
Future<void> showConfirmDialog(
  BuildContext context,
  String msg, {
  Key? key,
  VoidCallback? onConfirm,
  VoidCallback? onCancel,
}) {
  return showDialog(
    context: context,
    builder: (context) {
      return AlertDialog.adaptive(
        key: key,
        title: const Text('确认'),
        content: SingleChildScrollView(child: Text(msg)),
        actions: [
          TextButton(
            child: const Text('取消', style: TextStyle(color: Colors.black)),
            onPressed: () {
              NavigatorHelper.pop(context);
              onCancel?.call();
            },
          ),
          TextButton(
            child: const Text('确认'),
            onPressed: () {
              NavigatorHelper.pop(context);
              onConfirm?.call();
            },
          ),
        ],
      );
    },
  );
}

/// Selection Dialog
Future<void> showSelectionDialog({
  required BuildContext context,
  required String title,
  required List<SimpleDialogOption> selections,
  Key? key,
}) {
  return showDialog(
    context: context,
    builder: (context) =>
        SimpleDialog(key: key, title: Text(title), children: selections),
  );
}

/// SnackBar
void showSnackBar(
  BuildContext context,
  String msg, {
  String? actionLabel,
  VoidCallback? actionCallback,
}) {
  assert(
    (actionLabel.isNotNullOrEmpty && actionCallback == null) ||
        (actionLabel.isNotNullOrEmpty && actionCallback != null),
  );

  final snackBar = SnackBar(
    content: Text(msg),
    action: (actionLabel.isNotNullOrEmpty && actionCallback != null)
        ? SnackBarAction(label: actionLabel!, onPressed: actionCallback)
        : null,
  );

  ScaffoldMessenger.of(context).showSnackBar(snackBar);
}

/// Bottom Sheet
PersistentBottomSheetController showSelectionBottomSheet(
  BuildContext context,
  List<Widget> selections, {
  bool isHoriz = true,
  bool scrollable = false,
}) {
  return Scaffold.of(
    context,
  ).showBottomSheet((context) => getSelectionBottomSheetContent(selections));
}

/// Gets the widget for bottom sheet provides selections.
Widget getSelectionBottomSheetContent(
  List<Widget> selections, {
  bool isHoriz = true,
  bool scrollable = false,
}) {
  return Padding(
    padding: const EdgeInsets.all(10.0),
    child: isHoriz
        ? Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: selections,
          )
        : scrollable
        ? ListView(children: selections)
        : Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: selections,
          ),
  );
}

/// In-progress Overlay
void showInProgressOverlay(
  BuildContext context, {
  Widget icon = const Icon(Icons.sync, color: Colors.white70, size: 30.0),
  String message = '处理中',
  Widget? content,
}) {
  content ??= Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      SelfRotating(child: icon),
      const SizedBox(height: 10.0),
      Text(
        message,
        style: Theme.of(
          context,
        ).textTheme.titleSmall!.copyWith(color: Colors.white70),
      ),
    ],
  );

  showModalBottomSheet<void>(
    isScrollControlled: true,
    context: context,
    backgroundColor: Colors.transparent,
    builder: (context) => Center(
      child: Container(
        width: 120.0,
        height: 120.0,
        color: const Color.fromRGBO(50, 50, 50, 1.0),
        child: content,
      ),
    ),
  );
}

/// Date Picker
Future<DateTime?> showMyDatePicker(
  BuildContext context, {
  DateTime? initialDate,
  DateTime? firstDate,
  DateTime? lastDate,
}) {
  initialDate ??= getCurrentDateTime();
  firstDate ??= DateTime(2000);
  lastDate ??= DateTime(2100);

  if (initialDate.isAfter(lastDate)) initialDate = lastDate;
  if (initialDate.isBefore(firstDate)) initialDate = firstDate;

  return showDatePicker(
    context: context,
    initialDate: initialDate,
    firstDate: firstDate,
    lastDate: lastDate,
  );
}
